package msg

import (
	"context"
	"errors"
	"git.mycaigou.com/gfyx/common/rpc_client"
	"git.mycaigou.com/gfyx/micro-gfyx-api/infrastructure/common/constants"
	"git.mycaigou.com/gfyx/micro-gfyx-api/infrastructure/dto/base"
	pb "git.mycaigou.com/gfyx/micro-gfyx-api/proto/micro_gfyx_oppty_service_proto/micro-gfyx-oppty-service/base"
)

const MICRO_GFYX_OPPTY_SERVICE = "micro-gfyx-oppty-service"

type msgRepository struct {
}

// NewMsgRepository 构造函数
func NewMsgRepository() MsgRepositoryIface {
	return &msgRepository{}
}

func (r *msgRepository) MsgSetReaded(ctx context.Context, msgSetReadedParam *base.MsgSetReadedParam) (int64, error) {
	conn, ctx, err := rpc_client.GetClientConn(ctx, constants.MICRO_GFYX_OPPTY_SERVICE)
	if err != nil {
		return 0, err
	}
	defer func() {
		_ = conn.Close()
	}()

	req := &pb.MsgSetReadedRequest{}
	req.Id = msgSetReadedParam.Id

	client := pb.NewBaseMsgServiceClient(conn)
	resp, err := client.MsgSetReaded(ctx, req)
	if err != nil {
		return 0, err
	}
	return resp.GetId(), nil
}

// MsgDelete 删除消息
//func (r *msgRepository) MsgDelete(ctx context.Context, param *base.MsgDeleteParams) (int64, error) {
//	conn, ctx, err := rpc_client.GetClientConn(ctx, constants.MICRO_GFYX_OPPTY_SERVICE)
//	if err != nil {
//		return 0, err
//	}
//	defer func() {
//		_ = conn.Close()
//	}()
//
//	client := pb.NewBaseMsgServiceClient(conn)
//	resp, err := client.MsgDelete(ctx, &pb.MsgDeleteRequest{
//		MsgId: param.MsgId,
//	})
//	if err != nil {
//		return 0, err
//	}
//
//	return resp.GetId(), nil
//}

// MsgList 消息列表
func (r *msgRepository) MsgList(ctx context.Context, param *base.MsgListParam) (*base.MsgListData, error) {
	if param == nil {
		return nil, errors.New("缺少参数")
	}

	req := &pb.MsgListRequest{
		Page: &pb.BasePage{
			Page:     param.Page,
			PageSize: param.PageSize,
		},
		IsReaded: param.IsReaded,
	}

	conn, ctx, err := rpc_client.GetClientConn(ctx, constants.MICRO_GFYX_OPPTY_SERVICE)
	if err != nil {
		return nil, err
	}
	defer func() {
		_ = conn.Close()
	}()

	client := pb.NewBaseMsgServiceClient(conn)
	response, err := client.MsgList(ctx, req)
	if err != nil {
		return nil, err
	}

	msgList := make([]*base.MsgItem, 0)
	for _, item := range response.List {
		beCopyItem := &base.MsgItem{
			Id:           item.Id,
			MessageType:  item.MessageType,
			Title:        item.Title,
			Content:      item.Content,
			SenderId:     item.SenderId,
			ReceiverId:   item.ReceiverId,
			IsReaded:     item.IsReaded,
			ReceivedTime: item.ReceivedTime,
			ReadedTime:   item.ReadedTime,
			Link:         item.Link,
			CreatedBy:    item.CreatedBy,
			CreatedOn:    item.CreatedOn,
			ModifiedBy:   item.ModifiedBy,
			ModifiedOn:   item.ModifiedOn,
		}
		msgList = append(msgList, beCopyItem)
	}
	msgListData := &base.MsgListData{}
	if response.Page != nil {
		msgListData.Page = response.Page.Page
		msgListData.PageSize = response.Page.PageSize
		msgListData.Total = response.Page.Total
	}
	msgListData.GroupSummary = &base.GroupSummary{
		ReadedTotal:   response.GroupSummary.ReadedTotal,
		UnreadedTotal:   response.GroupSummary.UnreadedTotal,
		AllTotal:   response.GroupSummary.AllTotal,
	}
	msgListData.List = msgList
	return msgListData, nil
}

//// MsgImport 导入消息数据
//func (r *msgRepository) MsgImport(ctx context.Context) error {
//	conn, ctx, err := rpc_client.GetClientConn(ctx, constants.MICRO_GFYX_OPPTY_SERVICE)
//	if err != nil {
//		return err
//	}
//	defer func() {
//		_ = conn.Close()
//	}()
//	client := pb.NewBaseMsgServiceClient(conn)
//	_, err = client.ImportMsgs(ctx, &pb.ImportMsgRequest{})
//	if err != nil {
//		return err
//	}
//	return nil
//}
